import Vue from "vue";
import AppPopup from "./app-popup.vue";
import { Subject } from 'rxjs';

export default class Popup{

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof Popup
     */
    private static popup:any;
    
    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof Popup
     */
    private vm!: Vue;


    /**
     * 单例构造函数
     * 
     * @memberof Popup
     */
    private constructor() {
        if (Popup.popup) {
            return Popup.popup;
        }
    }
    
    /**
     * 静态获取单例对象方法
     *
     * @static
     * @returns {Popup}
     * @memberof Popup
     */
    public static getInstance(): Popup {
        if (!Popup.popup) {
            Popup.popup = new Popup();
        }
        return Popup.popup;
    }

    /**
     * 打开弹出层
     *
     * @param {{ componentName: string, position: string, CustomStyle?: object, round?: boolean, isFullScreen?: boolean}} props
     * componentName:组件名 position:弹出层位置 CustomStyle:自定义样式 round:是否圆角 isFullScreen:是否全屏
     * @param {*} viewdata 视图数据
     * @returns {Subject<any>}
     * @memberof Popup
     */
    public openPopup(props: { componentName: string, position: string, CustomStyle?: object, round?: boolean, isFullScreen?: boolean},viewdata: any): Subject<any> {
        try {
            Object.assign(props,{viewdata:viewdata});
            let component = AppPopup;
            const vm = new Vue({
                render(h) {
                    return h(component, { props });
                }
            }).$mount();
            this.vm = vm;
            document.body.appendChild(vm.$el);
            const comp:any = vm.$children[0];
            return comp.show();
        } catch (error) {
            console.log(error);
            return new Subject<any>();
        }
    }

}